package easy.id_0_100;
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 * Description: 上机编程认证
 * Note: 缺省代码仅供参考，可自行决定使用、修改或删除
 */

import java.util.Arrays;
import java.util.Comparator;

//雏鹰计划第六题
public class hua6 {
    public static void main(String[] args) {
        int[] array1 = {2, 2, 1, 3, 1};
        int[] array2 = {50, 20, 30, 10, 50};

        System.out.println(sorted(110, array2, array1));
    }

    public static int sorted(int cap, int[] billLen, int[] billPrior){
        // 创建索引数组并初始化
        Integer[] indexes = new Integer[billPrior.length];
        for (int i = 0; i < billPrior.length; i++) {
            indexes[i] = i;
        }

        // 定义比较器，首先按照数组一内容升序排序，相等时按照数组二内容升序排序
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                if (billPrior[a] != billPrior[b]) {
                    return billPrior[a] - billPrior[b];
                } else {
                    return billLen[a] - billLen[b];
                }
            }
        };

        // 使用自定义比较器对indexes数组进行排序
        Arrays.sort(indexes, comparator);
        int[] sortedArray1 = new int[billPrior.length];
        int[] originalIndexes = new int[billPrior.length];
        for (int i = 0; i < billPrior.length; i++) {
            sortedArray1[i] = billPrior[indexes[i]];
        }
        int sum=0;
        int result=0;
        for(int i=0;i<indexes.length; i++){
            sum+=billLen[indexes[i]];
            if(sum>cap){
                if(i+1<sortedArray1.length && sortedArray1[i]==sortedArray1[i-1]) return result;
                sum-=billLen[indexes[i]];
            }else{
                result++;
            }
        }
        return result;

    }
}



